<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/18 0018
 * Time: 10:03
 */

namespace app\admin\controller;


use think\Request;

class Advantage extends Base
{
    protected $_Advantage;
  
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Advantage = new \app\model\model\Advantage();
    }
    /*=======================功能优势===========================*/
    public function lists(){
        return $this->fetch('/application/advantagelist');
    }
    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        $data = $this->_Advantage->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            return $this->fetch('/application/advadd');
        }else{

            $data = [
                'name'=>input('name'),
                'content'=>input('editorValue'),
                'create_time'=>time(),
            ];
            $re = $this->_Advantage->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data = $this->_Advantage->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch('/application/advup');
        }else{
            $data = [
                'name'=>input('name'),
                'content'=>input('editorValue'),
            ];


            $re = $this->_Advantage->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Advantage->getOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 完全删除
     * */
    public function del(){
        $re = $this->_Advantage->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量完全删除
     * */
    public function delAll(){
        $re = $this->_Advantage->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 排序
     * */
    public function sort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_Advantage->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
}